<template>
  <div class="login-container">
    <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="0px" class="demo-ruleForm">
      <img src="../../images/logo.png" alt="" />
      <div class="title-container">
        <h3 class="title">mall管理平台</h3>
      </div>
      <el-form-item prop="username">
        <el-input type="text" v-model="ruleForm.username" autocomplete="on"></el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input v-model.number="ruleForm.password" autocomplete="on"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" style="width: 100%" @click="submitForm('ruleForm')">登录</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  data () {
    const validateEmail = (rule, value, callback) => {
      if (!value) {
        callback(new Error("请输入正确的账号"))
      } else {
        callback()
      }
    }
    const validatePass = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error("密码不能小于6位"))
      } else {
        callback()
      }
    }
    return {
      ruleForm: {
        username: "",
        password: "",
      },
      rules: {
        username: [{ validator: validateEmail, trigger: "blur" }],
        password: [{ validator: validatePass, trigger: "blur" }],
      },
    }
  },
  methods: {
    submitForm (formName) {
      this.$refs[formName].validate((valid) => {
        const userName = this.ruleForm.username === this.$const.username
        const password = this.ruleForm.password === this.$const.password
        if (valid && userName && password) {
          this.$message({ message: "登录成功", type: "success" })
          localStorage.setItem("userInfo", this.ruleForm)
          return this.$router.push({ name: "Layout" })
        } else {
          this.$message({ message: "账号密码不一致！", type: "error" })
          return false
        }
      })
    },
    resetForm (formName) {
      this.$refs[formName].resetFields()
    },
  },
}
</script>

<style scoped lang='less'>
.login-container {
  width: 100%;
  min-height: 100%;
  background-color: #7cacee;
  overflow: hidden;
}

.el-input {
  display: inline-block;
}

input {
  background: transparent;
  border: 0;
  -webkit-appearance: none;
  border-radius: 0;
  padding: 12px 5px 12px 15px;
  color: #fff;
  border: 1px solid rgba(255, 255, 255, 0.1);
  background: rgba(0, 0, 0, 0.1);
  border-radius: 5px;
  color: #454545;
}

.demo-ruleForm {
  width: 350px;
  margin: 50px auto;
}

.title-container {
  color: #fff;
  text-align: center;
}
</style>
